package com.geocalc;

import java.util.Scanner;

/**
 * 命令行交互式高程计算应用程序
 */
public class ElevationCalculatorApp {
    private DemReader demReader;
    private Scanner scanner;
    private String dataFilePath;
    
    /**
     * 构造函数
     */
    public ElevationCalculatorApp() {
        this.demReader = new DemReader();
        this.scanner = new Scanner(System.in);
        this.dataFilePath = null;
    }
    
    /**
     * 启动应用程序
     */
    public void start() {
        System.out.println("===== 高程计算器 =====");
        System.out.println("此程序用于根据经纬度计算高程值");
        System.out.println("请按照提示进行操作");
        
        boolean running = true;
        while (running) {
            showMenu();
            int choice = getUserChoice();
            
            switch (choice) {
                case 1:
                    loadDemData();
                    break;
                case 2:
                    calculateSingleElevation();
                    break;
                case 3:
                    showSamplePoints();
                    break;
                case 4:
                    showDataInfo();
                    break;
                case 5:
                    running = false;
                    System.out.println("感谢使用高程计算器，再见！");
                    break;
                default:
                    System.out.println("无效的选择，请重新输入");
            }
        }
        
        scanner.close();
    }
    
    /**
     * 显示菜单
     */
    private void showMenu() {
        System.out.println("\n请选择操作：");
        System.out.println("1. 加载DEM数据文件");
        System.out.println("2. 计算单个点的高程");
        System.out.println("3. 显示示例点的高程");
        System.out.println("4. 显示数据信息");
        System.out.println("5. 退出程序");
        System.out.print("请输入选项 (1-5): ");
    }
    
    /**
     * 获取用户选择
     * @return 用户选择的选项
     */
    private int getUserChoice() {
        while (true) {
            try {
                String input = scanner.nextLine();
                return Integer.parseInt(input);
            } catch (NumberFormatException e) {
                System.out.print("请输入有效的数字: ");
            }
        }
    }
    
    /**
     * 加载DEM数据文件
     */
    private void loadDemData() {
        System.out.print("请输入DEM数据文件路径: ");
        String filePath = scanner.nextLine();
        
        try {
            boolean success = demReader.loadDemData(filePath);
            if (success) {
                dataFilePath = filePath;
                System.out.println("DEM数据加载成功！");
            } else {
                System.out.println("DEM数据加载失败！");
            }
        } catch (ElevationException e) {
            System.out.println("加载数据时发生错误: " + e.getMessage());
        }
    }
    
    /**
     * 计算单个点的高程
     */
    private void calculateSingleElevation() {
        if (!demReader.isDataLoaded()) {
            System.out.println("请先加载DEM数据！");
            return;
        }
        
        try {
            System.out.print("请输入经度: ");
            double longitude = Double.parseDouble(scanner.nextLine());
            
            System.out.print("请输入纬度: ");
            double latitude = Double.parseDouble(scanner.nextLine());
            
            double elevation = demReader.calculateElevation(longitude, latitude);
            System.out.println("经纬度 (" + longitude + ", " + latitude + ") 的高程值为: " + elevation + " 米");
        } catch (NumberFormatException e) {
            System.out.println("请输入有效的数字！");
        } catch (ElevationException e) {
            System.out.println("计算高程时发生错误: " + e.getMessage());
        }
    }
    
    /**
     * 显示示例点的高程
     */
    private void showSamplePoints() {
        if (!demReader.isDataLoaded()) {
            System.out.println("请先加载DEM数据！");
            return;
        }
        
        // 预设一些示例点
        double[][] samplePoints = {
            {104.06, 30.67}, // 成都
            {108.95, 34.27}, // 西安
            {116.40, 39.90}, // 北京
            {121.47, 31.23}  // 上海
        };
        
        String[] pointNames = {"成都", "西安", "北京", "上海"};
        
        System.out.println("\n示例点高程计算结果：");
        
        for (int i = 0; i < samplePoints.length; i++) {
            double longitude = samplePoints[i][0];
            double latitude = samplePoints[i][1];
            
            try {
                double elevation = demReader.calculateElevation(longitude, latitude);
                System.out.println(pointNames[i] + " (" + longitude + ", " + latitude + "): " + elevation + " 米");
            } catch (ElevationException e) {
                System.out.println(pointNames[i] + " (" + longitude + ", " + latitude + "): 计算失败 - " + e.getMessage());
            }
        }
    }
    
    /**
     * 显示数据信息
     */
    private void showDataInfo() {
        if (!demReader.isDataLoaded()) {
            System.out.println("请先加载DEM数据！");
            return;
        }
        
        double[] info = demReader.getDataInfo();
        int ncols = (int) info[0];
        int nrows = (int) info[1];
        double xllcorner = info[2];
        double yllcorner = info[3];
        double cellsize = info[4];
        
        System.out.println("\nDEM数据信息：");
        System.out.println("数据文件路径: " + dataFilePath);
        System.out.println("网格大小: " + ncols + "x" + nrows);
        System.out.println("左下角坐标: (" + xllcorner + ", " + yllcorner + ")");
        System.out.println("单元格大小: " + cellsize);
        System.out.println("数据范围: 经度 [" + xllcorner + ", " + (xllcorner + ncols * cellsize) + "]");
        System.out.println("         纬度 [" + yllcorner + ", " + (yllcorner + nrows * cellsize) + "]");
    }
    
    /**
     * 主方法
     * @param args 命令行参数
     */
    public static void main(String[] args) {
        ElevationCalculatorApp app = new ElevationCalculatorApp();
        app.start();
    }
}